1
从传统代码到生成式人工智能应用
AI011Lesson 3
00:00

从传统代码到生成式人工智能应用

软件开发的格局正在经历根本性转变。我们正从僵化的、命令驱动的编程,转向灵活的、以自然语言为驱动的 生成式人工智能 交互。

1. 打破命令链

它是什么: 传统应用程序依赖于固定的图形用户界面(GUI)或特定的语言相关命令集。如果用户输入偏离预期,系统就会失败。

为什么重要: 生成式人工智能应用提供了前所未有的灵活性。它们允许用户通过自然语言进行交互,实现复杂目标,能够根据意图调整,而不仅仅是遵循语法。

2. 非确定性原则

它是什么: 在传统代码中,$1 + 1$ 始终等于 $2$。它是确定性的。 大型语言模型(LLMs)相反,它们基于概率运行。

其工作原理是: 对于完全相同的提示,它们可能产生不同的结果。这种多样性通过特定参数进行管理,其中最重要的是 温度

3. 核心组件:标记与温度

  • 标记: 模型使用的文本基本数值“构建块”。词语被分解成这些子词单元。
  • 温度: 一个设置(范围从 $0.0$ 到 $1.0$),用于控制随机性。低值产生可预测、专注的文本;高值则鼓励更具创造性和多样性的输出。
安全第一
切勿将 API 密钥直接硬编码到应用程序代码中。始终使用环境变量(例如, .env 文件)来保护您的 AI 资源免受未经授权的访问。
app.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
Question 1
Why are Large Language Models (LLMs) described as "non-deterministic"?
Because they can produce different results for the same prompt every time.
Because they always return the exact same output for a given input.
Because they cannot run on standard computer processors.
Because they require quantum computing to function.
Question 2
Which parameter should you decrease if you want the AI output to be more predictable and less creative?
Max Tokens
Top-P
Temperature
Frequency Penalty
Challenge: Building a "Study Buddy"
Apply your knowledge to a real-world scenario.
You are building a "Study Buddy" application that must provide strictly factual definitions for students preparing for exams. The application will connect to an Azure OpenAI resource.
Task 1
Identify the optimal Temperature setting for this specific task.
Solution:
Set Temperature to 0.0 or 0.1. This minimizes randomness and ensures the model provides the most likely, factual, and consistent definitions rather than creative or hallucinated responses.
Task 2
How should you secure the application's sensitive connection data?
Solution:
Move the API_KEY from the main code file into an environment variable or a hidden .env file. Use os.getenv("AZURE_OPENAI_KEY") to retrieve it securely at runtime.